<template>
  <el-dialog
    :model-value="dialogVisible"
    :title="dialogTitle"
    width="45%"
    @close="handleClose"
  >
    <el-form ref="formRef" :model="form" label-width="100px" :rules="rules">
      <el-form-item label="影视名" prop="title">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item label="影视季" prop="season">
        <el-input v-model="form.season" />
      </el-form-item>
      <el-form-item label="豆瓣源" prop="url" v-if="dialogTitle === '添加影视'">
        <el-input v-model="form.url" />
      </el-form-item>
      <el-form-item label="封面图" prop="img" v-if="dialogTitle === '添加影视'">
        <el-input v-model="form.img" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span
        class="dialog-footer"
        style="
          margin-left: 8%;
          margin-right: 8%;
          justify-content: space-between;
          display: flex;
        "
      >
        <el-button type="warning" @click="handleClose" plain>取消</el-button>
        <el-button type="primary" @click="handleConfirm" plain>确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { defineEmits, ref, defineProps, watch } from 'vue'
import { addAnime, editAnime } from '@/api/anime'
import { ElMessage } from 'element-plus'

const props = defineProps({
  dialogTitle: {
    default: '',
    type: String,
    required: true
  },
  dialogTableVable: {
    type: Object,
    default: () => {}
  }
})

const emits = defineEmits(['update:modelValue', 'initGetAnimesList'])

const handleClose = () => {
  emits('update:modelValue', false)
}

const handleConfirm = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      props.dialogTitle === '添加影视'
        ? await addAnime(form.value)
        : await editAnime({
            id: form.value.id,
            title: form.value.title,
            season: form.value.season,
            img: form.value.img
          })
      ElMessage({
        message: props.dialogTitle === '添加影视' ? '新增成功' : '修改成功',
        type: 'success'
      })
      emits('initGetAnimesList')
      handleClose()
    } else {
      return false
    }
  })
}

const formRef = ref(null)

const form = ref({
  id: 0,
  title: '',
  season: 0,
  url: '',
  img: ''
})

const rules = ref({
  title: [
    {
      required: true,
      pattern: '[^ \x20]+',
      message: '请输入影视名',
      trigger: 'blur'
    }
  ],
  season: [
    {
      required: true,
      message: '请输入影视的季',
      trigger: 'blur'
    },
    {
      type: 'integer',
      message: '影视的季必须是正整数',
      trigger: ['blur', 'change'],
      transform(value) {
        return value === '' ? 0 : Number(value)
      }
    }
  ],
  url: [
    {
      required: false,
      message: '请输入影视的豆瓣链接',
      trigger: 'blur'
    },
    {
      type: 'url',
      message: '请输入正确的URL链接地址',
      trigger: ['blur', 'change']
    }
  ],
  img: [
    {
      required: false,
      message: '请输入影视的封面链接',
      trigger: 'blur'
    },
    {
      type: 'url',
      message: '请输入正确的URL链接地址',
      trigger: ['blur', 'change']
    }
  ]
})

watch(
  () => props.dialogTableVable,
  () => {
    form.value = props.dialogTableVable
  },
  { deep: true, immediate: true }
)
</script>

<style lang="scss" scoped>
.el-dialog {
  border-radius: var(--el-border-radius-small);
}
</style>
